<?php
global $_W, $_GPC;
$op = empty($_GPC['op']) ? 'list' : $_GPC['op'];
$uid = intval($_GPC['uid']);
if (empty($uid)) {
    return $this->getresult(1, '请先登录', ['code' => 1001, 'msg' => '请先登录']);
}
/***校验token开始***/
if($uid){
    $token = $_GPC['token'];
    if (empty($token)) {
        return $this->getresult(1, '请重新登录', ['code' => 4001, 'msg' => '请重新登录']);
    }
    $token_result = $this->checktoken($uid, $token);
    if ($token_result == 1002) {
        return $this->getresult(1, '请重新登录', ['code' => 4002, 'msg' => '请重新登录']);
    } elseif ($token_result == 1003) {
        return $this->getresult(1, '请重新登录', ['code' => 4003, 'msg' => '请重新登录']);
    }elseif ($token_result == 1001) {
        //  更新token
        pdo_update($this->t_user_token,array('createtime'=>time()),array('uid' => $uid, 'token' => $token, 'weid' => $_W['uniacid']));
    }
}
/***校验token结束***/
if ($op == 'list') {
    $phone = pdo_getcolumn('goouc_class_user', array('weid' => $_W['uniacid'], 'id' => intval($uid)), 'phone');
    if (empty($phone)) {
        return $this->getresult(1, '请先绑定手机号');
    }
    $lessonlist = pdo_fetchall("select l.id,l.title,l.image from " . tablename('goouc_class_lesson_underline_student') . " s LEFT JOIN " . tablename($this->t_lesson) . " l ON s.lesson_id = l.id where s.weid = :weid and s.phone = :phone AND l.id is NOT NULL ", array(':weid' => $_W['uniacid'], ':phone' => $phone));
    if (!empty($lessonlist)) {
        foreach ($lessonlist as $k => $v) {
            if ($_W['uniacid'] == 20) {
                $lessonlist[$k]['image'] = $this->qiniu_url . $v['image'];
            } else {
                $lessonlist[$k]['image'] = $_W['attachurl'] . $v['image'];
            }
            $lessonlist[$k]['studynum'] = pdo_getcolumn('goouc_class_lesson_underline_student', array('weid' => $_W['uniacid'], 'lesson_id' => $v['id']), 'COUNT(id)');
            $time = pdo_fetchcolumn("select study_time from " . tablename($this->t_user_study_speed) . " where weid = :weid AND uid = :uid AND lessonid = :lessonid order by study_time DESC ", array(':weid' => $_W['uniacid'], ':uid' => $uid, ':lessonid' => $v['id']));
            if (!empty($time)) {
                $lessonlist[$k]['studytime'] = date('Y年m月d日 H:i:s', $time);
            } else {
                $lessonlist[$k]['studytime'] = 0;
            }
            if ($v['is_only'] == 1) {
                $son_vediotime = pdo_fetchcolumn("select vediotime from " . tablename($this->t_son) . " where lessonid = :lessonid ", array(':lessonid' => $v['id']));
                $lessonlist[$k]['speed'] = $lessonlist[$k]['speed'] / $son_vediotime;
            } else {
                $study_total = pdo_fetchcolumn("select COUNT(*) from " . tablename($this->t_user_study_speed) . " where weid = :weid AND uid = :uid AND lessonid = :lessonid ", array(':weid' => $_W['uniacid'], ':uid' => $uid, ':lessonid' => $v['id']));
                $total = pdo_fetchcolumn("select COUNT(*) from " . tablename($this->t_son) . " where  lessonid = :lessonid ", array(':lessonid' => $v['id']));
                if (empty($study_total) || empty($total)) {
                    $lessonlist[$k]['speed'] = 0;
                } else {
                    $lessonlist[$k]['speed'] = $study_total / $total;
                }
            }
        }
    }
    return $this->getresult(0, '获取数据', ['code' => 200, 'lessonlist'=>$lessonlist]);
}